package com.example.testapp.data

import androidx.room.TypeConverter
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId

/**
 * Room数据库日期转换器
 */
class DateConverters {
    companion object {
        /**
         * 将LocalDateTime转换为Long存储
         * @param dateTime LocalDateTime日期时间
         * @return 时间戳
         */
        @TypeConverter
        @JvmStatic
        fun fromLocalDateTime(dateTime: LocalDateTime?): Long? {
            return dateTime?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli()
        }

        /**
         * 将Long转换为LocalDateTime
         * @param timestamp 时间戳
         * @return LocalDateTime日期时间
         */
        @TypeConverter
        @JvmStatic
        fun toLocalDateTime(timestamp: Long?): LocalDateTime? {
            return timestamp?.let { LocalDateTime.ofInstant(Instant.ofEpochMilli(it), ZoneId.systemDefault()) }
        }
    }
} 